package com.aybived;

import android.R.bool;
import android.content.Context;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.interfaces.FullscreenActivity;
import com.view.Scene;

public class Game extends FullscreenActivity implements Runnable {
	
	private Scene gameview;
	public static Thread myThread;
	private Boolean stopThread;
	public static Boolean update = false;
	private long timeBefore = 0;
	public static Context gameContext;
	public static Game reference;
	public static int[] screenDimension;
	
	static private WindowManager windowManager;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameContext = this;
        
        windowManager=getWindowManager();
        
        //init screenDimensions
        screenDimension = new int[2];
        screenDimension[0] = windowManager.getDefaultDisplay().getWidth();
        screenDimension[1] = windowManager.getDefaultDisplay().getHeight();
        
        //init scene and setting as gameview
        gameview = new Scene( this );
        setContentView(gameview);
        
        //running seperate thread in this.run()
        stopThread = false;
        myThread = new Thread(this);
        myThread.start();    
        
        Game.reference = this;
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	Debug.stopMethodTracing();
    	stopThread = true;
    }
    
    public void endGame() {
    	stopThread = true;
    }
    
    public Handler updateHandler = new Handler(){
    	/** Gets called on every message that is received */
        // @Override
        @Override
		public void handleMessage(Message msg) {
            super.handleMessage(msg);
            double dt = msg.getData().getDouble("dt");
            gameview.update(dt);
        }
    };

	public void run() {
		timeBefore = System.currentTimeMillis();
		while(true) {
			if(stopThread)
				break;
			double dt = (double)(System.currentTimeMillis() - timeBefore) / 1000.0f;
			timeBefore = System.currentTimeMillis();
			if(update)
				gameview.update(dt);			
		}
	}

	static public int[] getScreenDimesions()
	{
		return screenDimension;
	}
}
